import { RegisterComponent } from "@feng3d/ecs";
import { Sprite } from "../../thirdparty/three/imports";
import { BufferGeometry3D } from "../core/BufferGeometry3D";
import { Component3D } from "../core/Component3D";
import { SpriteMaterial3D } from "../materials/SpriteMaterial3D";

declare module "@feng3d/ecs"
{
    interface ComponentMap { Sprite3D: Sprite3D; }
}

/**
 * 包装`three`中`Sprite`
 */
@RegisterComponent({ name: 'Sprite3D' })
export class Sprite3D extends Component3D
{
    private _sprite: Sprite;

    get geometry()
    {
        return this._geometry;
    }
    set geometry(v)
    {
        this._geometry = v;
        if (this._sprite)
        {
            this._sprite.geometry = this._geometry?._geometry;
        }
    }
    private _geometry: BufferGeometry3D;

    get material()
    {
        return this._material;
    }
    set material(v)
    {
        this._material = v;
        if (this._sprite)
        {
            this._sprite.material = this._material?._material;
        }
    }
    private _material: SpriteMaterial3D;

    init()
    {
        super.init();

        this._sprite = new Sprite(this.material?._material);

        this._entity._group.add(this._sprite);
        this._entity.isSprite = true;
    }

    destroy(): void
    {
        this._entity._group.remove(this._sprite);
        this._sprite = null;
        this._entity.isSprite = false;

        super.destroy();
    }
}
